package storage.transaction.recovery;

import static storage.transaction.recovery.LogRecord.*;
import java.util.Iterator;
import log.BasicLogRecord;
import server.SimpleDB;

class LogRecordIterator implements Iterator<LogRecord> {
	private Iterator<BasicLogRecord> iter = SimpleDB.logManager().iterator();

	public boolean hasNext() {
		return iter.hasNext();
	}

	public LogRecord next() {
		BasicLogRecord rec = iter.next();
		int op = rec.nextInt();
		switch (op) {
		case CHECKPOINT:
			return new CheckpointRecord(rec);
		case START:
			return new StartRecord(rec);
		case END:
			return new StopRecord(rec);
		case ROLLBACK:
			return new RollbackRecord(rec);
		case COMMIT:
			return new CommitRecord(rec);
		case SETINT:
			return new SetIntRecord(rec);
		case SETSTRING:
			return new SetStringRecord(rec);
		case SETLONG:
			return new SetLongRecord(rec);
		case SETDOUBLE:
			return new SetDoubleRecord(rec);
		case SETFLOAT:
			return new SetFloatRecord(rec);
		case SETBOOLEAN:
			return new SetBooleanRecord(rec);
		case SETBYTE:
			return new SetByteRecord(rec);
		case SAFE:
			return new SafeRecord(rec);
		case UNDO:
			return new UndoRecord(rec);
		case REDO:
			return new RedoRecord(rec);

		default:
			throw new Error("Undefined rec: " + op);
		}
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}
}
